<?php

/*
 * CopyRight  : (C)2012-2099 HaoTeam Inc.
 * Document   : ModelTrait.php
 * Created on : 2021-3-22 9:50:24
 * Author     : Tiger <1192851302@qq.com>
 * Description: This is NOT a freeware, use is subject to license terms.
 *              这即使是一个免费软件,使用时也请遵守许可证条款,得到当时人书面许可.
 *              未经书面许可,不得翻版,翻版必究;版权归属 HaoTeam Inc;
 */

namespace haoteam\traits;

use think\facade\Db;

/**
 * 模型基础
 * Class ModelTrait
 * Tiger <1192851302@qq.com> 2021-3-22 9:50:24
 */
trait ModelTrait {

    /**
     * 开启事务
     * @author Tiger <1192851302@qq.com>
     */
    public static function beginTrans() {
        Db::startTrans();
    }

    /**
     * 提交事务
     * @author Tiger <1192851302@qq.com>
     */
    public static function commitTrans() {
        Db::commit();
    }

    /**
     * 关闭事务
     * @author Tiger <1192851302@qq.com>
     */
    public static function rollbackTrans() {
        Db::rollback();
    }

    /**
     * 根据结果提交滚回事务
     * @author Tiger <1192851302@qq.com>
     * @param $res
     */
    public static function checkTrans($res) {
        if ($res) {
            self::commitTrans();
        } else {
            self::rollbackTrans();
        }
    }

    /**
     * 
     * @author Tiger <1192851302@qq.com> 
     * @param type $where
     * @return type
     */
    public static function be($where) {
        return self::where($where)->count();
    }

    /**
     * 插入全部
     * @author Tiger <1192851302@qq.com>
     * @param type $data
     * @param type $last_return
     * @return boolean
     */
    public static function insertAll($data, $last_return = false) {
        $return = [];
        foreach ($data as $item) {
            $return[] = self::create($item);
        }
        if ($last_return) {
            return $return;
        }
        return true;
    }

    /**
     * 有就更新没有就插入
     * @author Tiger <1192851302@qq.com>
     * @param type $data
     * @return type
     */
    public static function insert_into($data) {
        $model = new self;
        if (isset($data[$model->pk]) == false) {//没有主键直接新增
            return self::create($data);
        }
        $where = [$model->pk => $data[$model->pk]];
        if (self::be($where)) {//有就更新
            self::update($data, $where);
            return $data;
        } else {
            unset($data[$model->pk]);
            return self::create($data);
        }
    }

    /**
     * 
     * @author Tiger <1192851302@qq.com>
     * @param type $data
     * @param type $alias
     * @return type
     */
    public static function auto_alias_table_top($data, $alias) {
        $items = [];
        foreach ($data as $key => $item) {
            if (strpos($key, '.') === false) {
                $items[$alias . '.' . $key] = $item;
                continue;
            }
            $items[$key] = $item;
        }
        return $items;
    }

    /**
     * 
     * @author Tiger <1192851302@qq.com>
     * @param type $data
     * @param type $alias
     * @return type
     */
    public static function auto_alias_table_field($data, $alias) {
        $items = [];
        foreach ($data as $key => $item) {
            if (strpos($item, '.') === false) {
                $items[$key] = $alias . '.' . $item;
                continue;
            }
            $items[$key] = $item;
        }
        return $items;
    }

}
